<?php

namespace App\Providers;

use Dreamcat\ConfigClient\Api\Impl\NacosServer;
use Dreamcat\ConfigClient\Api\NacosServerInterface;
use Dreamcat\ConfigClient\Popo\NacosAuthorization;
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;

/**
 * -
 * @author vijay
 */
class NacosServerProvider extends ServiceProvider
{
    /** @inheritDoc */
    public function register()
    {
        $this->app
            ->bind(
                NacosServerInterface::class,
                function () {
                    $server = new NacosServer(
                        config("configCenter.nacos.url"),
                        config("configCenter.nacos.decoders", [])
                    );
                    $server->setLogger($this->app->get(LoggerInterface::class));
                    $server->setPullWaitTimeoutMs(config("configCenter.nacos.pullTime", 30000))
                            ->setQueryTimeoutMs(config("configCenter.nacos.queryTime", 10000));
                    $user = env("NACOS_USERNAME");
                    $password = env("NACOS_PASSWORD");
                    if ($user && $password) {
                        $auth = new NacosAuthorization();
                        $auth->setUserName($user)->setPassword($password);
                        $server->setAuthorization($auth);
                    }
                    return $server;
                }
            );
    }
}

# end of file
